package com.ps.bicyclecharging.controller;

import com.ps.dto.Pay;
import com.ps.bicyclecharging.service.ChargingService;
import com.ps.domain.Breakdown;
import com.ps.domain.Recharge;
import com.ps.domain.ResultInfo;
import com.ps.utils.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author:Lisa
 * @create:2019/08/13
 */
@RestController
//@RequestMapping("/charging")
public class ChargingController {

    @Autowired
    private ChargingService chargingService;


    /**
     * 设置支付密码
     *      传入参数uid、password
     * @return
     */
    @PostMapping("/payment/password")
    public ResultInfo setPaymentPsw(@RequestParam Map map){
        long uid = (long) map.get("uid");
        String password = (String) map.get("password");

        chargingService.setPaymentPsw(uid,password);

        return MyUtils.getReturn(null);
    }


    /**
     * 修改支付密码
     *      传入参数uid、password、newPassword
     * @return
     */
    @PutMapping("/payment/password")
    public ResultInfo updatePaymentPsw(@RequestParam Map map){
        long uid = (long) map.get("uid");
        String password = (String) map.get("password");
        String newPassword = (String) map.get("newPassword");

        chargingService.updatePaymentPsw(uid, password, newPassword);

        return MyUtils.getReturn(null);
    }


    /**
     * 查询计费规则
     * @return
     */
    @GetMapping("/billing/rules")
    public ResultInfo queryBillingRules(){
        //查询余额
        String rules = chargingService.queryBillingRules();

        return MyUtils.getReturn(rules);
    }


    /**
     * 查询账户余额
     * @param uid
     * @return
     */
    @GetMapping("/queryBalanceByUid/{uid}")
    public ResultInfo queryBalanceByUid(@PathVariable long uid){
        //查询余额
        double balance = chargingService.queryBalanceByUid(uid);

        return MyUtils.getReturn(balance);
    }




    /**
     * 上报计费异常
     * @param breakdown
     * @return
     */
    @PostMapping("/billingexceptions")
    public ResultInfo billingExceptions(@RequestBody Breakdown breakdown){
        //插入一条故障记录
        chargingService.billingExceptions(breakdown);

        return MyUtils.getReturn(null);
    }


    /**
     * 充值接口
     * @param recharge  包含参数money、payMethod、type
     * @return
     */
    @PostMapping("/recharge/{uid}")
    public ResultInfo recharge(@RequestBody Recharge recharge, @PathVariable long uid){

        chargingService.recharge(recharge);

        return MyUtils.getReturn(null);
    }



    /**
     * 支付接口
     *      （充值押金，骑行）
     */
    @PostMapping("/payment")
    public ResultInfo pay(@RequestBody Pay pay){

        chargingService.pay(pay);

        return MyUtils.getReturn(null);
    }



    /**
     * 查询充值记录
     * @Param id 用户id
     *      state 状态 1.查支付记录，2.查充值记录，3.全部流水
     */
    @GetMapping("/rechargeRecords")
    public ResultInfo queryRechargeRecords(@RequestParam long uid, String state){

        List<Recharge> list = chargingService.queryRechargeRecords(uid, state);

        return MyUtils.getReturn(list);
    }

    /**
     * 查询押金
     * @Param id 用户id
     */
    @GetMapping("/deposit")
    public ResultInfo queryDeposit(@RequestParam long id){

        double money = chargingService.queryDeposit(id);

        return MyUtils.getReturn(money);
    }

    /**
     * 设置免密支付
     * @Param id 用户id
     * @Param password 用户支付密码
     */
    @PutMapping("/densityFree")
    public ResultInfo densityFree(@RequestParam long id, String password){

        chargingService.densityFree(id, password);

        return MyUtils.getReturn(null);
    }


    /**
     * 申请退还押金
     * @Param id 用户id
     */
    @PutMapping("/returnDeposit")
    public ResultInfo returnDeposit(@RequestParam long id){

        chargingService.returnDeposit(id);

        return MyUtils.getReturn(null);
    }


}
